import { Component, OnInit, Injector } from '@angular/core';
import { DetailBase } from 'app/modules/base/detail-base';
import { map, switchMap } from 'rxjs/operators';
import { SoftwareInfo, SoftwareService } from '../../services/software.service';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'm-details',
  templateUrl: './details.component.html',
  styleUrls: ['./details.component.scss'],
})
export class DetailsComponent extends DetailBase implements OnInit {
  constructor(
    private injector: Injector,
    private softwareService: SoftwareService,
  ) {
    super(injector);
  }
  result$ = this.getData<SoftwareInfo>('details');
  editing = false;
  area_id: number;
  ngOnInit() {
    this.route.queryParams.subscribe(params => {
      if (params.area_id) {
        this.area_id = params.area_id;
      }
    });
  }

  async delete(software: SoftwareInfo) {
    const params = {
      software_id: software.id,
      area_id: software.area_id ? software.area_id : Number(this.area_id),
    };
    const result = await this.sweet.confirm(`确定要移除软件${software.name}吗？`);
    if (result.value) {
      await this.softwareService.delete([params]);
      this.back();
    }
  }

  saved() {
    this.editing = false;
    this.reload();
  }
}
